﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.UI;
using FwProject.Core.Restaurant;

namespace FwProject.Manager
{
    public class LoadingCarManager: IDomainService
    {
        private readonly IRepository<LoadingCarInfo, Guid> _loadingcarRepository;
        public LoadingCarManager(IRepository<LoadingCarInfo, Guid> loadingcarRepository)
        {
            _loadingcarRepository = loadingcarRepository;

        }
        /// <summary>
        /// 添加一个装车明细
        /// </summary>
        /// <param name="loadingCar"></param>
        /// <returns></returns>
        public async Task<Guid> CreateLoadingcar(LoadingCarInfo loadingCar)
        { 
           
            return await _loadingcarRepository.InsertAndGetIdAsync(loadingCar);
        }

        public LoadingCarInfo CreateSync(LoadingCarInfo loadingCar)
        {
            var model= _loadingcarRepository.GetAll().Where(o => o.LoadCarId == loadingCar.Id && o.TypeNumberInfoId == loadingCar.TypeNumberInfoId).FirstOrDefault();
            if (model != null)
                throw new UserFriendlyException("当前车辆已经装过此型号");


            return  _loadingcarRepository.Insert(loadingCar);
        }
        /// <summary>
        ///修改一个装车明细
        /// </summary>
        /// <param name="loadingCar"></param>
        /// <returns></returns>
        public async Task UpdateLoadingcar(LoadingCarInfo loadingCar)
        {

            await _loadingcarRepository.UpdateAsync(loadingCar);
        }

        public LoadingCarInfo GetByTypeNumberId(Guid typeNumberId,Guid loadCarId)
        {

            return _loadingcarRepository.GetAll().Where(m => m.TypeNumberInfoId == typeNumberId && m.LoadCarId==loadCarId).FirstOrDefault();
        }
        /// <summary>
        ///  delete 一个装车明细
        /// </summary>
        /// <param name="loadingCar"></param>
        /// <returns></returns>
        public async Task DeleteLoadingcar(LoadingCarInfo loadingCar)
        {

            await _loadingcarRepository.DeleteAsync(loadingCar);
        }
    }
}
